package com.shadow.baiqun.delegate.send.order;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shadow.baiqun.R;
import com.shadow.baiqun.R2;
import com.shadow.baiqun.delegate.send.adapter.OrderAdapter;
import com.shadow.baiqun.view.TableHeadView;
import com.shadow.core.delegate.RxShadowDelegate;
import com.shadow.core.net.callback.IError;
import com.shadow.core.net.callback.ISuccess;
import com.shadow.core.net.rx.RxRestClient;
import com.shadow.ui.recycler.BaseDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author zhangyu
 * @date 2018/7/26
 */

public class OrderDelegate extends RxShadowDelegate {
    private static final String TASK_ID = "taskId";


    public static OrderDelegate create(String taskId) {
        final OrderDelegate contentDelegate = new OrderDelegate();
        final Bundle bundle = new Bundle();
        bundle.putString(TASK_ID, taskId);
        contentDelegate.setArguments(bundle);
        return contentDelegate;
    }

    private static final int PAGE_SIZE = 50;
    @BindView(R2.id.toolbar)
    Toolbar mToolbar;
    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R2.id.lv_title)
    TableHeadView mHeadView;
    private OrderAdapter mOrderAdapter;
    private String mTaskId;
    private boolean isRequesting = false;
    private int mNextRequestPage = 1;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mTaskId = bundle.getString(TASK_ID);
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_send_order;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mToolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
            mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    pop();
                }
            });
            mToolbar.setTitle("出库指示单");
        }
        List<String> list = new ArrayList<>();
        list.add("零件码");
        list.add("总量");
        list.add("批次");
        list.add("包装数");
        list.add("库位");
        mHeadView.setHeaders(list);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.addItemDecoration(BaseDecoration.create(getContext().getResources()
                .getColor(R.color.color_white_light_gray), 1));
        mOrderAdapter = new OrderAdapter(R.layout.item_send_order, null);
        mOrderAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        }, mRecyclerView);
        mOrderAdapter.setEmptyView(R.layout.view_loading, mRecyclerView);
        if (mOrderAdapter.getEmptyView() != null) {
            mOrderAdapter.getEmptyView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    refresh();
                }
            });
        }
        mRecyclerView.setAdapter(mOrderAdapter);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        refresh();
    }

    private void refresh() {
        if (!isRequesting) {
            mNextRequestPage = 1;
            isRequesting = true;
            mOrderAdapter.setEmptyView(R.layout.view_loading, mRecyclerView);
            requestData(true);
        }

    }

    private void requestData(final boolean isRefresh) {
        mDisposables.add(RxRestClient.builder()
                .url("WMS/backstage/sendCar/outOrder")
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        mOrderAdapter.setEmptyView(R.layout.view_empty, mRecyclerView);
                        setData(isRefresh, new OrderDataConverter()
                                .convert(response));
                        isRequesting = false;
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        mOrderAdapter.setEmptyView(R.layout.view_empty, mRecyclerView);
                        isRequesting = false;
                    }
                })
                .params("taskid", mTaskId)
                .params("pageNum", mNextRequestPage)
                .params("pageSize", PAGE_SIZE)
                .build()
                .unRestGet());
    }

    private void setData(boolean isRefresh, List<OrderEntity> data) {
        mNextRequestPage++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mOrderAdapter.setNewData(data);
        } else {
            if (size > 0) {
                mOrderAdapter.addData(data);
            }
        }
        if (size < PAGE_SIZE) {
            //第一页如果不够一页就不显示没有更多数据布局
            mOrderAdapter.loadMoreEnd(isRefresh);
        } else {
            mOrderAdapter.loadMoreComplete();
        }
    }

    private void loadMore() {
        requestData(false);
    }
}
